import * as api from '../../util/api';
import http from '../../util/http';
import el from "element-ui/src/locale/lang/el";
import da from "element-ui/src/locale/lang/da";

const state ={
    //订货单数据
    ordernumber:'',
    money:'',
    name:'',
    address:'',
    phone:'',
    orderdata:[],
    allorderconut:0,
    //详情页数据
    orderdetails:{
        creator: "",
        deliveryDate: "",
        orderAuditStatus: 0,
        orderMainPrice: 1000,
        orderNumber:"",
        receivingInfo: {
            clientName: "",
            receivingName: "",
            receivingTel: 0,
            receivingAddress: ""
        },
        orderDetailsList:[]
    },
    //客户地址数据
    khaddress:{
        clientName: "",           //客户名称
        receivingAddress: "",      //收货地址
        receivingInfoId: "",       //收货信息id
        receivingName: "",       //收货人
        receivingTel: 0     //手机
    },
    /*渲染商品数据*/
    goodsData:[],

    //退货单数据
    reorderdata:[],
    reallcount:0,
    reorderdetaildata:{},

    reordernumber:'',
    remoney:'',
    retime:'',
    reinfor:'',
    rephone:'',
};
const mutations ={
    addorderdata(state,res){
        console.log(res.data.data)
        state.orderdata=res.data.data.orders
        state.orderdata.map((item,i)=>{
            if(item.out===0){
                item.out="未出库"
            }if(item.out===1){
                item.out="已出库"
            }if(item.receipt===0){
                item.receipt='未发货'
            }if(item.receipt===1){
                item.receipt='已发货'
            }if(item.orderAuditStatus===0){
                item.orderAuditStatus='订单审核'
            }if(item.orderAuditStatus===1){
                item.orderAuditStatus='财务审核'
            }if(item.orderAuditStatus===2){
                item.orderAuditStatus='出库审核'
            }if(item.orderAuditStatus===3){
                item.orderAuditStatus='发货审核'
            }if(item.orderAuditStatus===4){
                item.orderAuditStatus='发货确认'
            }if(item.orderAuditStatus===5){
                item.orderAuditStatus='订单收货确认'
            }if(item.orderAuditStatus===10){
                item.orderAuditStatus='已作废'
            }if(item.collectStatus===0){
                item.collectStatus='未付款'
            }if(item.collectStatus===1){
                item.collectStatus='待审核'
            }if(item.collectStatus===2){
                item.collectStatus='已付款'
            }
            return item
        })
        state.allorderconut=res.data.data.totalCount
    },
    addorderdetails(state,res){
        state.orderdetails.creator=res.creator
        state.orderdetails.deliveryDate=res.deliveryDate
        state.orderdetails.orderAuditStatus=res.orderAuditStatus
        state.orderdetails.orderMainPrice=res.orderMainPrice
        state.orderdetails.orderNumber=res.orderNumber
        state.orderdetails.receivingInfo.clientName=res.receivingInfo.clientName
        state.orderdetails.receivingInfo.receivingName=res.receivingInfo.receivingName
        state.orderdetails.receivingInfo.receivingTel=res.receivingInfo.receivingTel
        state.orderdetails.receivingInfo.receivingAddress=res.receivingInfo.receivingAddress
        state.orderdetails.orderDetailsList=res.orderDetailsList
    },
    address(state,data){
        state.khaddress.clientName=data.clientName
        state.khaddress.receivingAddress=data.receivingAddress
        state.khaddress.receivingInfoId=data.receivingInfoId
        state.khaddress.receivingName=data.receivingName
        state.khaddress.receivingTel=data.receivingTel
    },
    addgooddata(state,data){
        state.goodsData=data
        console.log(state.goodsData)
    },
    addreordernumber(state,obj){
        state.reordernumber=obj.reordernumber
        state.remoney=obj.remoney
        state.retime=obj.retime
        state.reinfor=obj.reinfor
        state.rephone=obj.rephone
    },
    addreorderdata(state,obj){
        console.log(obj.data.data)
        state.reorderdata=obj.data.data.orders
        state.reorderdata.map((item,i)=>{
            if(item.outStatus===0){
                item.outStatus='未付款'
            }if(item.outStatus===1){
                item.outStatus='已付款'
            }if(item.orderAuditStatus===6){
                item.orderAuditStatus='退单审核'
            }if(item.orderAuditStatus===7){
                item.orderAuditStatus='退单收货确认'
            }if(item.orderAuditStatus===8){
                item.orderAuditStatus='退款确认'
            }if(item.orderAuditStatus===9){
                item.orderAuditStatus='收款确认'
            }if(item.orderAuditStatus===10){
                item.orderAuditStatus='已作废'
            }
            return item
        })
        state.reallcount=obj.data.data.totalCount

    },
    addreorderdetails(state,obj){
        state.reorderdetaildata=obj
        console.log(state.reorderdetaildata)
    },
};
const actions={
    /*订单请求数据*/
    getOrderData({commit},obj){
        let clientId=obj.clientId
        let condition=obj.condition
        return http({
            url:api.orderControllerorderList,
            method:'post',
            data:{
                clientId,
                condition
            }
        }).then((res)=>{
            console.log(res)
            commit('addorderdata',res)
            return res.data.data
        })
    },
    /*订单详情数据*/
    changeordernumber({commit},obj){
        console.log(obj)
        let {clientId,orderMainId}=obj
        return http({
            url:api.orderControllerorderDetail,
            method:'post',
            data:{
                clientId,orderMainId
            }
        }).then(res=>{
            commit('addorderdetails',res.data.data)
        })

    },
    /*订单作废*/
    getzfOrder({commit},obj){
        let orderMainId=obj
        return http({
            url:api.orderControllercancelOrder,
            method:'post',
            data:{orderMainId}
        }).then(res=>{
            return res.data.code
        })
    },
    /*查询客户地址数据*/
    getKHaddress({commit},obj){
        let {clientId,orderMainId}=obj
        return http({
            url:api.receivingControllerqueryAddress,
            method:'post',
            data:{clientId,orderMainId}
        }).then(res=>{
            console.log(res)
            commit('address',res.data.data)
        })
    },
    /*改地址*/
    changekHaddress({commit},obj){
        let {clientId,
            clientName,
            receivingAddress,
            receivingInfoId,
            receivingName,
            receivingTel}=obj
        return http({
            url:api.receivingControllerupdateAddress,
            method:'post',
            data:{
                clientId,
                clientName,
                receivingAddress,
                receivingInfoId,
                receivingName,
                receivingTel
            }
        }).then(res=>{
            return res.data.code
        })
    },
    //查商品
    getGoodsData({commit}){
        return http({
            url:api.addordergoods,
            method:'post',
            data:{
                currentPage:1,
                perSize:1000
            }
        }).then(res=>{
            let data=res.data.data.goodsList
            console.log(data)
            data.map((item,i)=>{
                item.num=1
                return item
            })
            commit('addgooddata',data)
            return res
        })
    },
    //新增订单
    addOrder({commit},obj){
        let {
            clientId,   //客户id，通过登录获取
            creator,                //制单人
            deliveryDate,     //交货日期
            orderDetailsList,
            orderMainPrice,         //订单金额
            receivingInfoId  //收货信息id
        }=obj
      return http({
          url:api.orderControlleraddOrder,
          method:'post',
          data:{
              clientId,   //客户id，通过登录获取
              creator,                //制单人
              deliveryDate,     //交货日期
              orderDetailsList,
              orderMainPrice,         //订单金额
              receivingInfoId  //收货信息id
          }
      }).then(res=>{
          return res.data.code
      })
    },
    /*查余额*/
    accountmoney({commit},obj){
        let {clientId,orderMainId}=obj
        return http({
            url:api.orderControllerqueryAccountInfo,
            method:'post',
            data:{clientId,orderMainId}
        }).then(res=>{
            return res.data.data
        })
    },
    /*付款接口*/
    fukuanmoney({commit},obj){
        let { collectAccount,
            notes,
            orderMainId,
            payMoney,
            payStatus,
            payStatusId}=obj
        return http({
            url:api.orderControllerpay,
            method:'post',
            data:{
                collectAccount,
                notes,
                orderMainId,
                payMoney,
                payStatus,
                payStatusId
            }
        }).then(res=>{
            console.log(res)
            return res.data.code
        })
    },

    /*退货单数据*/
    getReorderData({commit},obj){
        let clientId=obj.clientId
        let condition=obj.condition
        return http({
            url:api.returnOrderControllerqueryReturnOrderList,
            method:'post',
            data:{
                clientId,
                condition
            }
        }).then((res)=>{
            console.log(res)
            commit('addreorderdata',res)
            return res.data.data
        })
    },
    /*新增退货单数据*/
    addreorder({commit},obj){
        let {
            clientId,   //客户id
            fiance,/*这里不需要，新版本上线不能删*/
            orderDetailsList,
            orderMainPrice       //订单金额
        }=obj
        return http({
            url:api.reOrderAdd,
            method:'post',
            data:{clientId,fiance, orderDetailsList, orderMainPrice}
            }
            ).then(res=>{
                return res.data.code
        })
    },
    /*退货单详情数据*/
    reorderdatadetails({commit},obj){
        let {orderNumber}=obj
        return http({
            url:api.returnOrderControllerqueryRreturnOrderDetail,
            method:'post',
            data:{orderNumber}
        }).then(res=>{
            console.log(res)
            commit('addreorderdetails',res.data.data)
            return res.data.code
        })
    },
    /*退货单作废*/
    zfreorder({commit},obj){
      let {orderNumber}=obj
        return http({
            url:api.reOrderCancel,
            method:'post',
            data:{orderNumber}
        }).then(res=>{
            return res.data.code
        })
    },


    changereordernumber({commit},obj){
        console.log(obj)
        commit('addreordernumber',obj)
    }
};
const getters ={
    getordernumder(state){
        return Number(state.ordernumber)
    },
    getmoney(state){
        return Number(state.money)
    },
    getname(state){
        return state.name
    },
    getaddress(state){
        return state.address
    },
    getphone(state){
        return state.phone
    },
    getreordernumder(state){
        return Number(state.reordernumber)
    },
    getremoney(state){
        return Number(state.remoney)
    },
    getretime(state){
        return state.retime
    },
    getreinfor(state){
        return state.reinfor
    },
    getrephone(state){
        return state.rephone
    },

    getreorderdata(state){
        return state.reorderdata
    },
    getreallcount(state){
        return state.reallcount
    },
    getreorderdatadetails(state){
        return state.reorderdetaildata
    },

    getorderdata(state){
        return state.orderdata
    },
    getordercount(state){
        return state.allorderconut
    },
    getorderdetails(state){
        return state.orderdetails
    },
    getkhaddress(state){
        return state.khaddress
    },
    getgoodata(state){
        return state.goodsData
    }
};
export default{
    state,
    actions,
    mutations,
    getters
}